﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SH.Common.CommonReturn;
using SH.Multitenant.API.Filters;
using SH.Multitenant.Application.IServices.IRole;
using SH.Multitenant.Domain.RBAC;
using System.DirectoryServices.ActiveDirectory;

namespace SH.Multitenant.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly IRoleServices _roleServices;

        public RoleController(IRoleServices roleServices)
        {
            _roleServices = roleServices;
        }

        /// <summary>
        /// 条件分页获取角色列表
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult PageQuery(string? roleName, int pageSize = 10, int pageIndex = 1)
        {
            var result = _roleServices.PageQuery(roleName, pageSize, pageIndex);
            return Ok(result);
        }
        /// <summary>
        /// 获取角色列表（下拉框）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetAllRoleListAsync()
        {
            var result = await _roleServices.All.ToListAsync();
            ApiResult<List<Role>> apiResult = new ApiResult<List<Role>>();
            apiResult.Success(result);
            return Ok(apiResult);
        }
        /// <summary>
        /// 角色添加       
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [LogActionFilter("Top", "角色添加")]
        public async Task<IActionResult> RoleAddAsync(RoleDTO dto)
        {
            var result = await _roleServices.RoleAddAsync(dto);
            ApiResult<int> apiResult = new ApiResult<int>();
            if (result > 0)
            {
                apiResult.Success();
            }
            else
            {
                apiResult.Error(ApiResult<int>.ErrorType.Request, "添加失败");
            }
            return Ok(apiResult);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        [HttpPost]
        [LogActionFilter("Top", "角色批量删除")]
        public IActionResult BatchDelRoles(List<Role>? roles)
        {
            var result = _roleServices.BatchDelRoles(roles);
            ApiResult<int> apiResult = new ApiResult<int>();
            if (result > 0)
            {
                apiResult.Success();
            }
            else
            {
                apiResult.Error(ApiResult<int>.ErrorType.Request, "删除失败");
            }
            return Ok(apiResult);
        }
    }
}
